unit ServerAPI;

interface

uses
  IdHTTP;

type
  TServerApi = class
  private
    FIdHttp: TIdHttp;
    function Post(AUrl: String;AParams,AValues:array of String): String;
  public
    constructor Create;
    destructor Destroy;override;
    function UserRegist(APhone,APassword,APasswordConfirm,ACaptcha: String): String;
    function UserLogin(APhone,APassword: String): String;
    function UserGetCaptcha(APhone: String): String;
    function UserForgetPassword(APhone,APassword,APasswordConfirm,ACaptcha: String): String;
    function UserGetInfomation(AUid: String): String;
    function UserGetProgress(AUid: String): String;
    function ServerTime: String;
    function GetTable: String;
    function StartTraining(AUid: String): String;
    function UpdateTrain(AUid,ATableId,ATrainId,AProgress: String): String;
    function TrainVersion(ATrainId:String): String;
    function MainVersion:String;
    function TrainLog(AUid,ATrainId,AXmlVal: String): String;
    function UpdateLog(AUid, ALogInfo: string): string;
    function GetLog(AUid: string): string;

    function AddTrain(AName,AVer,AResourceURL,AProgramURL: String): String;
    function RefreshTrain(AId,AName,AVer,AResourceURL,AProgramURL: String): String;
    function DeleteTrain(ATrainId: String): String;
    function TrainList: String;

    

  end;


implementation

uses
  Classes, SysUtils, Windows, XMLIntf, XMLDoc, CommonUtils, MD5;
{ TServerApi }

function TServerApi.AddTrain(AName, AVer, AResourceURL, AProgramURL: String): String;
var ApiUrl: String;
    Params,Values: array[0..4] of String;
begin
  Params[0]:='name';
  Values[0]:=AName;
  Params[1]:='program';
  Values[1]:=AProgramURL;
  Params[2]:='resource';
  Values[2]:=AResourceURL;
  Params[3]:='time';
  Values[3]:=IntToStr(GetTickCount);
  Params[4]:='version';
  Values[4]:=AVer;
  ApiUrl:='http://123.57.240.210/index.php?g=api&m=train&a=add';
  Result:=Post(ApiUrl,Params,Values);
end;

function TServerApi.RefreshTrain(AId, AName, AVer, AResourceURL, AProgramURL: String): String;
var ApiUrl: String;
    Params,Values: array[0..5] of String;
begin
  Params[0]:='id';
  Values[0]:=AId;
  Params[1]:='name';
  Values[1]:=AName;
  Params[2]:='program';
  Values[2]:=AProgramURL;
  Params[3]:='resource';
  Values[3]:=AResourceURL;
  Params[4]:='time';
  Values[4]:=IntToStr(GetTickCount);
  Params[5]:='version';
  Values[5]:=AVer;
  ApiUrl:='http://123.57.240.210/index.php?g=api&m=train&a=update';
  Result:=Post(ApiUrl,Params,Values);
end;


constructor TServerApi.Create;
begin
  FIdHttp:=TIdHttp.Create;
end;

function TServerApi.DeleteTrain(ATrainId: String): String;
var ApiUrl: String;
    Params,Values: array[0..4] of String;
begin
  Params[0]:='id';
  Values[0]:=ATrainid;
  Params[1]:='time';
  Values[1]:=IntToStr(GetTickCount);
  ApiUrl:='http://123.57.240.210/index.php?g=api&m=train&a=del';
  Result:=Post(ApiUrl,Params,Values);
end;

destructor TServerApi.Destroy;
begin
  FIdHttp.Free;
  inherited;
end;


function TServerApi.Post(AUrl: String; AParams, AValues: array of String): String;
var DataToSend: TStringList;
    i: Integer;
    TokenStr,TokenHash: String;
begin
  DataToSend:=TStringList.Create;
  TokenStr:='';
  for i := 0 to Length(AParams)-1 do begin
    DataToSend.Add(AParams[i]+'='+AValues[i]);
    TokenStr:= TokenStr+AParams[i]+AValues[i];
  end;
  TokenStr:=TokenStr+'`@2015~@*TTT~na*()';
  TokenHash:=LowerCase(StrMD5(TokenStr));
  DataToSend.Add('token='+TokenHash);
  Result:= UTF8ToAnsi(FIdHttp.Post(AUrl, DataToSend));
  DataToSend.Free;
end;

function TServerApi.ServerTime: String;
var ApiUrl: String;
    Params,Values: array[0..0] of String;
begin
  Params[0]:='time';
  Values[0]:=IntToStr(GetTickCount);
  ApiUrl:='http://123.57.240.210/index.php?g=api&m=sys&a=sysdate';
  Result:=Post(ApiUrl,Params,Values);
end;

function TServerApi.StartTraining(AUid: String): String;
var ApiUrl: String;
    Params,Values: array[0..1] of String;
begin
  Params[0]:='time';
  Values[0]:=IntToStr(GetTickCount);
  Params[1]:='uid';
  Values[1]:=AUid;
  ApiUrl:='http://123.57.240.210/index.php?g=api&m=table&a=start';
  Result:=Post(ApiUrl,Params,Values);
end;

function TServerApi.TrainList: String;
var ApiUrl: String;
    Params,Values: array[0..0] of String;
begin
  Params[0]:='time';
  Values[0]:=IntToStr(GetTickCount);
  ApiUrl:= 'http://123.57.240.210/index.php?g=api&m=train&a=glist';
  Result:=Post(ApiUrl,Params,Values);
end;

function TServerApi.TrainLog(AUid, ATrainId, AXmlVal: String): String;
var ApiUrl: String;
    Params,Values: array[0..3] of String;
begin
  Params[0]:='time';
  Values[0]:=IntToStr(GetTickCount);
  Params[1]:='trainid';
  Values[1]:=ATrainId;
  Params[2]:='uid';
  Values[2]:=AUid;
  Params[3]:='xmlval';
  Values[3]:=AXmlVal;
  ApiUrl:='http://123.57.240.210/index.php?g=api&m=train&a=log';
  Result:=Post(ApiUrl,Params,Values);
end;

function TServerApi.TrainVersion(ATrainId:String): String;
var ApiUrl: String;
    Params,Values: array[0..1] of String;
begin
  Params[0]:='time';
  Values[0]:=IntToStr(GetTickCount);
  Params[1]:='trainid';
  Values[1]:=ATrainId;
  ApiUrl:='http://123.57.240.210/index.php?g=api&m=train&a=ver';
  Result:=Post(ApiUrl,Params,Values);
end;

function TServerApi.GetLog(AUid: string): string;
var
  ApiUrl: String;
  Params,Values: array[0..4] of String;
  iDoc: IXMLDocument;
  iNode: IXMLNode;
  slLogs: TStringList;
  I: Integer;
begin
  Params[0]:='uid';
  Values[0]:=AUid;
  ApiUrl:='http://123.57.240.210/index.php?g=api&m=users&a=getlog';
  try
    Result:=Post(ApiUrl,Params,Values);
    iDoc := LoadXMLData(Result);
    slLogs := TStringList.Create;
    try
      for I := 0 to iDoc.DocumentElement.ChildNodes.Count - 1 do
      begin
        if SameText(iDoc.DocumentElement.ChildNodes[I].NodeName, 'message') then
        begin
          iNode := iDoc.DocumentElement.ChildNodes[I].ChildNodes.FindNode('log');
          if iNode <> nil then
            slLogs.Add(iNode.NodeValue);
        end;
      end;
      Result := slLogs.Text;
    finally
      FreeAndNil(slLogs);
    end;
  except
    Result := '';
  end;
end;

function TServerApi.GetTable: String;
var ApiUrl: String;
    Params,Values: array[0..0] of String;
begin
  Params[0]:='time';
  Values[0]:=IntToStr(GetTickCount);
  ApiUrl:='http://123.57.240.210/index.php?g=api&m=table&a=show';
  Result:=Post(ApiUrl,Params,Values);
end;

function TServerApi.MainVersion: String;
var ApiUrl: String;
    Params,Values: array[0..0] of String;
begin
  Params[0]:='time';
  Values[0]:=IntToStr(GetTickCount);
  ApiUrl:='http://123.57.240.210/index.php?g=api&m=sys&a=ver';
  Result:=Post(ApiUrl,Params,Values);
end;

function TServerApi.UpdateLog(AUid, ALogInfo: string): string;
var
  ApiUrl: String;
  Params,Values: array[0..4] of String;
begin
  Params[0]:='uid';
  Params[1]:='log';
  Values[0]:=AUid;
  Values[1]:=ALogInfo;
  ApiUrl:='http://123.57.240.210/index.php?g=api&m=users&a=updatelog';
  Result:=Post(ApiUrl,Params,Values);
end;

function TServerApi.UpdateTrain(AUid, ATableId, ATrainId,
  AProgress: String): String;
var
  ApiUrl: String;
  Params,Values: array[0..4] of String;
begin
  Params[0]:='progress';
  Params[1]:='tableid';
  Params[2]:='time';
  Params[3]:='trainid';
  Params[4]:='uid';
  Values[0]:=AProgress;
  Values[1]:=ATableId;
  Values[2]:=IntToStr(GetTickCount);
  Values[3]:=ATrainId;
  Values[4]:=AUid;
  ApiUrl:='http://123.57.240.210/index.php?g=api&m=table&a=updatetrain';
  Result:=Post(ApiUrl,Params,Values);
end;

function TServerApi.UserForgetPassword(APhone, APassword, APasswordConfirm,
  ACaptcha: String): String;
var ApiUrl: String;
    Params,Values: array[0..4] of String;
begin
  Params[0]:='captcha';
  Params[1]:='password';
  Params[2]:='passwordconfirm';
  Params[3]:='phone';
  Params[4]:='time';
  Values[0]:=ACaptcha;
  Values[1]:=APassword;
  Values[2]:=APasswordConfirm;
  Values[3]:=APhone;
  Values[4]:=IntToStr(GetTickCount);
  ApiUrl:='http://123.57.240.210/index.php?g=api&m=users&a=changepass';
  Result:=Post(ApiUrl,Params,Values);
end;

function TServerApi.UserGetCaptcha(APhone: String): String;
var DataToSend: TStringList;
    ApiUrl: String;
    Params,Values: array[0..1] of String;
begin
  Params[0]:='phone';
  Values[0]:=APhone;
  Params[1]:='time';
  Values[1]:=IntToStr(GetTickCount);
  ApiUrl:='http://123.57.240.210/index.php?g=api&m=users&a=captcha';
  Result:=Post(ApiUrl,Params,Values);
end;

function TServerApi.UserGetInfomation(AUid: String): String;
var ApiUrl: String;
    Params,Values: array[0..1] of String;
begin
  Params[0]:='time';
  Values[0]:=IntToStr(GetTickCount);
  Params[1]:='uid';
  Values[1]:=AUid;
  ApiUrl:='http://123.57.240.210/index.php?g=api&m=users&a=getuserinfo';
  Result:=Post(ApiUrl,Params,Values);

end;

function TServerApi.UserGetProgress(AUid: String): String;
var ApiUrl: String;
    Params,Values: array[0..1] of String;
begin
  Params[0]:='time';
  Params[1]:='uid';
  Values[0]:=IntToStr(GetTickCount);
  Values[1]:=AUid;
  ApiUrl:='http://123.57.240.210/index.php?g=api&m=table&a=profile';
  Result:=Post(ApiUrl,Params,Values);
end;

function TServerApi.UserLogin(APhone, APassword: String): String;
var ApiUrl: String;
    Params,Values: array[0..2] of String;
begin
  Params[0]:='password';
  Params[1]:='time';
  Params[2]:='username';
  Values[0]:=APassword;
  Values[1]:=IntToStr(GetTickCount);
  Values[2]:=APhone;
  ApiUrl:='http://123.57.240.210/index.php?g=api&m=users&a=login';
  Result:=Post(ApiUrl,Params,Values);
end;

function TServerApi.UserRegist(APhone, APassword,APasswordConfirm,ACaptcha: String): String;
var ApiUrl: String;
    Params,Values: array[0..4] of String;
begin
  Params[0]:='captcha';
  Params[1]:='password';
  Params[2]:='passwordconfirm';
  Params[3]:='phone';
  Params[4]:='time';
  Values[0]:=ACaptcha;
  Values[1]:=APassword;
  Values[2]:=APasswordConfirm;
  Values[3]:=APhone;
  Values[4]:=IntToStr(GetTickCount);
  ApiUrl:= 'http://123.57.240.210/index.php?g=api&m=users&a=reg';
  Result:=Post(ApiUrl,Params,Values);
end;

end.
